﻿using Ivory;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ivory.GUI;
using Ivory.Libs;
using CrystalCore;
using Jitter;
using Jitter.Collision;
using CrystalCore.Libs;
using CrystalCore.Mapping;

namespace Barva
{
    public abstract class TestBase : GameScreen
    {
        //Fields
        private AxisIndicator _AxisIndicator;
        private TextButton mainMenuBtn;

        //Construct
        public TestBase(Game game)
            : base(game)
        {

        }

        //Methods 
        public override void RenderGame(SpriteBatch batch, GameTime time)
        {
            ObjectHolder.GlobalDebugRenderer.Draw(ObjectHolder.GameCamera, time);
            _AxisIndicator.Draw(ObjectHolder.GameCamera, time);
            RenderTestScreen(batch, time);
        }
        public override void UpdateGame(GameTime time, InputControl input)
        {
            ObjectHolder.GameCamera.UpdateCamera(input, time);
            SelectionEngine.SolveSelection(Game, input);
            UpdateTestScreen(time, input);
        }

        public override void RenderUI(SpriteBatch batch, GameTime time)
        {
            batch.Begin();
            mainMenuBtn.DrawComponent(batch, time);
            batch.End();
        }        
        public override void UpdateUI(GameTime time, InputControl input)
        {
            mainMenuBtn.UpdateComponent(input, time);
        }

        public abstract void RenderTestScreen(SpriteBatch batch, GameTime time);
        public abstract void UpdateTestScreen(GameTime time, InputControl input);
        public abstract void LoadTest();

        public override bool PreloadAssets()
        {
            this.LoadingScreen = new LoadScreen(Game, "fonts/menu", Color.White, Color.Black);

            return true;
        }
        public override bool LoadData()
        {
            //Assign Globals
            ObjectHolder.GameWorld = new World(new CollisionSystemSAP());
            ObjectHolder.GameWorld.Gravity = new Jitter.LinearMath.JVector(0);

            ObjectHolder.GameCamera = new CrystalCore.Libs.RenderTestCamera(Game);
            ObjectHolder.GlobalDebugRenderer = new CrystalCore.Libs.DebugDrawer(Game);
            ObjectHolder.GlobalDebugRenderer.Initialize();
                      
            _AxisIndicator = new AxisIndicator(Game, false, 1f);
            _AxisIndicator.Setup();

            mainMenuBtn = new TextButton(Game, Resources.Fonts["menu"], "Main Menu", new Vector2(0, 0))
            {
                ButtonColor = Color.Yellow,
                ButtonHoverColor = Color.Red,
                EnableShadowing = true
            };
            mainMenuBtn.LoadComponent();
            mainMenuBtn.LeftClicked += (s, e) =>
                {
                    Manager.SwitchToMainMenu();
                };

            LoadTest();

            return true;
        }
    }
}
